﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;
using System.Web;

namespace YouHP.Pages.User.Group
{
    public class MemberAdmin: GroupBasePage
    {
        protected List<GroupUserInfo> AdminList = null;
        protected List<GroupUserInfo> MemberList = null;
        protected List<GroupUserInfo> ApplyList = null;
        protected int PageCount = 1;
        protected int PageIndex = 1;
        protected int PageSize = 15;
        protected int MemberCount = 0;
        protected int n = 5;//可设的管理员数目
        protected string PageNumberString = "";

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected new void HandleCommon()
        {
            this.AddLocation("圈子成员管理", "/User/Group/Memeber.aspx");
        }

        protected new void HandlePost()
        {
            int type = RequestTools.GetFormInt("formtype", 0);
            if (type == 1) HandleApplyForm();
            else if (type == 2) HandleAdminForm();
            else if (type == 3) HandleMemberForm();
        }

        protected void HandleApplyForm()
        {
            int applytype = RequestTools.GetFormInt("applytype", 0);
            string idstr = RequestTools.GetFormString("applyid");
            int[] idArray = Utils.StrToIntArray(idstr, ",");
            bool succ = false;
            if (applytype == 1)  // 通过审批
            {
                succ = GroupUserBLL.Instance.UpdateGroupUserRole(GroupData.Id, idArray, Constants.RoleMember);
            }
            else if (applytype == 2)　// 忽略
            {
                succ = GroupUserBLL.Instance.UpdateGroupUserRole(GroupData.Id, idArray, Constants.RoleIngore);
            }
            if (succ)
            {
                this.AddMsgLine("操作成功");
                SetUrl("/User/Group/Member.aspx?gid=" + GroupData.Id);
                SetMetaRefresh();
            }
            else
            {
                this.AddErrLine("操作失败");
            }
        }

        protected void HandleAdminForm()
        {
            int admintype = RequestTools.GetFormInt("admintype", 0);
            string idstr = RequestTools.GetFormString("adminid");
            int[] idArray = Utils.StrToIntArray(idstr, ",");
            bool succ = false;
            if (admintype == 1)  // 将为普通成员
            {
                succ = GroupUserBLL.Instance.UpdateGroupUserRole(GroupData.Id, idArray, Constants.RoleMember);
            }
            else if (admintype == 2)　// 删除
            {
                succ = GroupUserBLL.Instance.UpdateGroupUserRole(GroupData.Id, idArray, Constants.RoleDeleted);
            }
            if (succ)
            {
                this.AddMsgLine("操作成功");
                SetUrl("/User/Group/Member.aspx?gid=" + GroupData.Id);
                SetMetaRefresh();
            }
            else
            {
                this.AddErrLine("操作失败");
            }
        }

        protected void HandleMemberForm()
        {
            int membertype = RequestTools.GetFormInt("membertype", 0);
            string idstr = RequestTools.GetFormString("memberid");
            int[] idArray = Utils.StrToIntArray(idstr, ",");
            bool succ = false;
            if (membertype == 1)  // 提升为管理员
            {
                if (GroupUserBLL.Instance.GetGroupUserListAdmin(GroupUserData.Id).Count < n)
                {
                    succ = GroupUserBLL.Instance.UpdateGroupUserRole(GroupData.Id, idArray, Constants.RoleAdmin);
                }
                else
                {
                    this.AddErrLine("管理员数目已达最大！");
                    this.SetShowBackLink(true);
                }
            }
            else if (membertype == 2)　// 删除
            {
                succ = GroupUserBLL.Instance.UpdateGroupUserRole(GroupData.Id, idArray, Constants.RoleDeleted);
            }
            if (succ)
            {
                this.AddMsgLine("操作成功");
                SetUrl("/User/Group/Member.aspx?gid=" + GroupData.Id);
                SetMetaRefresh();
            }
            else
            {
                this.AddErrLine("操作失败");
            }
        }

        protected new void HandleGet()
        {
            PageIndex = RequestTools.GetQueryInt("page", 1);
#if GENDATA
            for(int i=0;i<3;i++)
            {
                GroupUserInfo info =new GroupUserInfo();
                info.UserAvatar="";
                info.UserName="张三";
                info.JoinTime=DateTime.Now;
                info.Role = Constants.RoleAdmin;
                AdminList.Add(info);
              
         
            }
              for(int i=0;i<5;i++)
            {
                GroupUserInfo info =new GroupUserInfo();
                info.UserAvatar="";
                info.UserName="张三";
                info.JoinTime=DateTime.Now;
                info.Role = Constants.RoleMember;
                MemberList.Add(info);
           
            }
#else
            AdminList = GroupUserBLL.Instance.GetGroupUserListAdmin(GroupData.Id);
            MemberList = GroupUserBLL.Instance.GetGroupUserListMember(GroupData.Id, PageSize, PageIndex, out MemberCount);
            ApplyList = GroupUserBLL.Instance.GetGroupUserListAppling(GroupData.Id);
#endif

            PageCount = Utils.GetPageCount(MemberCount,PageSize);

            string query = HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            PageNumberString = this.GetPagingString(PageIndex, PageCount,
                "/User/Group/Member.aspx" + query, "page");

        }
    }
}